using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
	class UserPointBalanceDA {

        private const string SP_Insert = "UserPointBalance_Insert";

        public static UserPointBalanceEnt CreateFromReader(IDataReader reader) {
            UserPointBalanceEnt item = new UserPointBalanceEnt();
            item.ID = ConvertUtils.ToLong(reader["ID"]);
            item.UserKey = ConvertUtils.ToString(reader["UserKey"]);
            item.RefUserKey = ConvertUtils.ToString(reader["RefUserKey"]);
            item.Action = (EUsePointBalanceActionType)ConvertUtils.ToShort(reader["Action"]);
            item.PointUsed = ConvertUtils.ToShort(reader["PointUsed"]);
            item.CoinUsed = ConvertUtils.ToShort(reader["CoinUsed"]);
            item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
            return item;
        }

        public static int Insert(UserPointBalanceEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
            int result = -1;
            Database db = Database.CreateDatabase(connectionName);
            using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
                db.AddParameterOuter(cmd, "ID", DbType.Int64);
                db.AddParameter(cmd, "UserKey", item.UserKey);
                db.AddParameter(cmd, "RefUserKey", item.RefUserKey);
                db.AddParameter(cmd, "Action", (short)item.Action);
                db.AddParameter(cmd, "PointUsed", item.PointUsed);
                db.AddParameter(cmd, "CoinUsed", item.CoinUsed);
                db.AddParameter(cmd, "DateCreated", item.DateCreated);

                if (dbCon != null && dbTrans != null) {
                    result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
                    item.ID = ConvertUtils.ToLong(cmd.Parameters["ID"].Value);
                }
                else {
                    result = db.ExecuteNonQuery(cmd);
                    item.ID = ConvertUtils.ToLong(cmd.Parameters["ID"].Value);
                }
            }
            return result;
        }


	}
}